package com.goods.shop.service.ems;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.goods.shop.service.AbstractService;
import com.goods.shop.service.model.ems.ResponseTraceModel;
import com.goods.shop.service.model.ems.TraceModel;
import com.goods.shop.util.HttpUtils;
import com.goods.shop.util.TimeUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmsServiceImpl extends AbstractService implements EmsService {
    @Override
    public String getTraceNo(String traceNo) {
        ResponseTraceModel responseModel=new ResponseTraceModel();
        responseModel.setResponseState(true);
        responseModel.setErrorDesc("查询结果正常返回！");
        responseModel.setReceiveID("JDPT");
        List<TraceModel> traces=new ArrayList<>();
        for(int i=0;i<10;i++){
            TraceModel trace=new TraceModel();
            trace.setTraceNo(traceNo);
            trace.setOpTime(TimeUtils.formatter(LocalDateTime.now()));
            trace.setOpOrgProvName("山东"+i);
            trace.setOpOrgCity("潍坊"+i);
            trace.setOpOrgCode("23802300"+i);
            trace.setOpOrgName("安徽省巢湖市大宗揽收班"+i);
            trace.setOpCode("OpCode:"+i);
            trace.setOpDesc("离开【安徽省巢湖市大宗揽收班】,下一站【邮政大连邮件处理中心");
            traces.add(trace);
        }
        responseModel.setResponseItems(traces);

        String result= JSONObject.toJSONString(responseModel);

        return result;
    }

    /**
     * 调用查询路径
     * @param traceNo
     * @return
     */
    @Override
    public List<TraceModel> triggerTraceNo(String traceNo) {
        List<TraceModel> traces=new ArrayList<>();
        String url="http://211.156.201.13:7001/emstrace/zd";
        Map<String,Object> params=new HashMap<>();
        params.put("type","json");
        params.put("traceNo",traceNo);
        String result= HttpUtils.requestGet(url,params);
        logger.info("result="+result);
        JSONObject object=JSONObject.parseObject(result);
        boolean responseState=object.getBoolean("responseState");
        if(responseState){
            JSONArray responseItems=object.getJSONArray("responseItems");
            traces=responseItems.toJavaList(TraceModel.class);
        }
        return traces;
    }

    public static void main(String[] args) {
        EmsServiceImpl service=new EmsServiceImpl();
        for(int i=0;i<1;i++){
//            System.out.println("ssss");
//            try {
//                Thread.sleep(10000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            String traceNo="1150970185730";
            List<TraceModel> traces=service.triggerTraceNo(traceNo);
//            logger.info("execute times:"+i+"==========================================");
            for(TraceModel trace:traces){
//                logger.info(JSONObject.toJSONString(trace));
            }

        }
        //1150970185730
    }
}
